package com.woniuxy.controller;


import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.*;
import com.woniuxy.model.Defect;
import com.woniuxy.service.DefectService;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.UploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Api(tags = "缺陷接口")
@RestController
@RequestMapping("/defect")
public class DefectController {

    @Resource
    private DefectService defectService;

    /**
     * 获取自己上报项目名和id
     * @param uId
     * @return
     */
    @ApiOperation("获取自己上报项目名和id")
    @GetMapping("/getDefectProjectName/{uId}")
    public Result getDefectProjectName(@PathVariable Integer uId){   //获取缺陷表中的项目名字和id

        List<ProjectIdAndNameInfo> projectIdAndNameInfos=defectService.getDefectProjectNameAndId(uId);

        return Result.success(200,"查询成功",projectIdAndNameInfos);
    }

    /**
     * 获取所有有效优先级，严重程度，分类
     * @return
     */
    @ApiOperation("获取所有有效优先级，严重程度，分类")
    @PostMapping("/getDefectAllDate")
    public Result getDefectAllDate(){
        DefectAllDateInfo defectAllDateInfo=defectService.getDefectAllDate();

        return Result.success(200,"查询成功",defectAllDateInfo);
    }

    /**
     * 上传文件，并将网络路径返回
     * @param file
     * @return
     */
    @ApiOperation("上传文件，并将网络路径返回")
    @PostMapping(value = "uploadFile",produces = "application/json;charset=UTF-8")
    public Result uploadFile(@RequestParam("file") MultipartFile file){


        String url = UploadFile.uploadImg(file);

        return Result.success(200,"上传成功",url);
    }

    /**
     * 添加缺陷表，添加附件表
     * @param addDefectFrom
     * @return
     */
    @ApiOperation("添加缺陷表，添加附件表")
    @PostMapping("addDefect")
    public Result addDefect(@RequestBody AddDefectFrom addDefectFrom){

        defectService.addDefect(addDefectFrom);

        return Result.success(200,"缺陷上报成功",null);
    }

    /**
     * 获取我的缺陷信息
     * @param defectConditionForm
     * @return
     */
    @ApiOperation("获取我的缺陷信息")
    @PostMapping("getMyDefect")
    public Result getMyDefect(@RequestBody DefectConditionForm defectConditionForm){
        System.out.println(defectConditionForm);
        List<DefectAndFileInfo> defectAndFileInfos=defectService.getMyDefect(defectConditionForm);

        return Result.success(200,"查询成功",defectAndFileInfos);
    }

    /**
     * 根据缺陷id逻辑删除缺陷
     * @param id
     * @return
     */
    @ApiOperation("根据缺陷id逻辑删除缺陷")
    @GetMapping("/removeDefect/{id}")
    public Result removeDefect(@PathVariable Integer id){

        defectService.removeDefect(id);

        return Result.success(200,"删除成功",null);
    }

    /**
     * 根据缺陷id修改缺陷
     * @param updateDefectForm
     * @return
     */
    @ApiOperation("根据缺陷id修改缺陷")
    @PostMapping("changeDefect")
    public Result changeDefect(@RequestBody UpdateDefectForm updateDefectForm){
        System.out.println(updateDefectForm);
        defectService.changeDefect(updateDefectForm);
        return Result.success(200,"更新成功",null);
    }

    /**
     * 获取我管理的项目的新建的缺陷信息
     * @param defectConditionForm
     * @return
     */
    @ApiOperation("获取我管理的项目的新建的缺陷信息")
    @PostMapping("getNewDefectByMyProject")
    public Result getNewDefectByMyProject(@RequestBody DefectConditionForm defectConditionForm){
        List<DefectAndFileInfo> defectAndFileInfos=defectService.getNewDefectByMyProject(defectConditionForm);

        return Result.success(200,"查询成功",defectAndFileInfos);
    }

    /**
     * 获取该缺陷对应项目的员工
     * @param projectId
     * @return
     */
    @ApiOperation("获取该缺陷对应项目的员工")
    @GetMapping("/getProjectMan/{projectId}")
    public Result getProjectMan(@PathVariable Integer projectId){
        List<UserAndRoleInfo> userAndRoleInfos=defectService.getProjectMan(projectId);

        return Result.success(200,"查询成功",userAndRoleInfos);
    }

    /**
     * 指派员工修改
     * @param choiceUserForm
     * @return
     */
    @ApiOperation("指派员工修改")
    @PostMapping("/choiceMan")
    public Result choiceMan(@RequestBody ChoiceUserForm choiceUserForm){
        defectService.choiceMan(choiceUserForm);
        return Result.success(200,"分配成功",null);
    }

    /**
     * 获取我管理的项目的分配完成的缺陷信息
     * @param defectConditionForm
     * @return
     */
    @ApiOperation("获取我管理的项目的分配完成的缺陷信息")
    @PostMapping("getNoNewDefectByMyProject")
    public Result getNoNewDefectByMyProject(@RequestBody DefectConditionForm defectConditionForm){
        List<DefectAndFileInfo> defectAndFileInfos=defectService.getNoNewDefectByMyProject(defectConditionForm);

        return Result.success(200,"查询成功",defectAndFileInfos);
    }

    /**
     * 获取分配给我的缺陷信息
     * @param defectConditionForm
     * @return
     */
    @ApiOperation("获取分配给我的缺陷信息")
    @PostMapping("/getGiveMeDefect")
    public Result getGiveMeDefect(@RequestBody DefectConditionForm defectConditionForm){
        List<DefectAndFileInfo> defectAndFileInfos=defectService.getGiveMeDefect(defectConditionForm);
        return Result.success(200,"查询成功",defectAndFileInfos);
    }

    /**
     * 员工接收任务
     * @param acceptDefectForm
     * @return
     */
    @ApiOperation("员工接收任务")
    @PostMapping("/isAcceptDefect")
    public Result isAcceptDefect(@RequestBody AcceptDefectForm acceptDefectForm){
        defectService.isAcceptDefect(acceptDefectForm);
        return Result.success(200,"接收成功",null);
    }

    /**
     * 员工处理缺陷
     * @param handleDefectForm
     * @return
     */
    @ApiOperation("员工处理缺陷")
    @PostMapping("handleDefect")
    public Result handleDefect(@RequestBody HandleDefectForm handleDefectForm){
        defectService.handleDefect(handleDefectForm);

        return Result.success(200,"缺陷处理完成",null);
    }

    /**
     * 反馈缺陷信息
     * @param responseDefectForm
     * @return
     */
    @ApiOperation("反馈缺陷信息")
    @PostMapping("responseDefect")
    public Result responseDefect(@RequestBody ResponseDefectForm responseDefectForm){
        defectService.responseDefect(responseDefectForm);

        return Result.success(200,"缺陷反馈完成",null);
    }

}

